import fw from '../framework.js'
module.exports = {
    pay(orderPayId, successFn, failFn) {
        var self = this;
        fw.ajax('wxPay/applet-unified-order', { orderPayId: orderPayId }).then(function (cRes) {
            var d = cRes.data;
            wx.requestPayment(
                {
                    'timeStamp': d.timeStamp,
                    'nonceStr': d.nonceStr,
                    'package': d.package,
                    'signType': d.signType,
                    'paySign': d.paySign,
                    'success': function (res) {
                        if (successFn) successFn(res,function(){
                            self.getLoginInfo();
                        });
                    },
                    'fail': function (res) {
                        if (failFn) failFn(res);
                    },
                    'complete': function (res) {
                        console.log('complete', res);
                    }
                })
        });
    },
    getLoginInfo(){
        fw.ajax('merchant/login-info',{}).then(res=>{
            fw.user.set(res.data);
        })
    },
    getGoodsName(data) {
        var goods = data.goods;
        var quota = JSON.parse(goods.property);
        return goods.goodsCount + '台设备,' + quota.quota + ['天', '月', '年'][((quota.quotaType | 0) - 1)];
    },
    getType(operate) {
        return [
            {
                name: '购买',
                value: '1'
            }, {
                name: '续费',
                value: '2'
            }, {
                name: '升级',
                value: '3'
            }
        ][(operate | 0) - 1].name;
    },
    // 订单状态  支付  未支付
    getStatus(data) {
        if (data.orderState == 0) {// 订单状态正常
            if (data.refundState == 0) { // 未退款
                if (data.payState == 1) {  // 已支付
                    return '已支付'
                } else { // 待支付
                    return '待支付'
                }
            } else {
                return '已退款';
            }
        } else {
            return '已取消';
        }
    },
    // 是否未支付
    isUnPay(data) {
        return data.orderState == 0 && data.refundState == 0 && data.payState == 0;
    },
    // 是否已付款 未退款
    isPayed(data) {
        return data.orderState == 0 && data.refundState == 0 && data.payState == 1;
    },

}